﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Xml;
using System.Xml.Schema;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace XML.Ultils
{
    public class Validate
    {

        private bool check;
        public bool Check
        {
            get { return check; }
            set { check = value; }
        }

        public void validates(string xsd, FileStream xml)
        {

            this.check = false;
            XmlReaderSettings booksSettings = new XmlReaderSettings();

            booksSettings.Schemas.Add(null, xsd);
            booksSettings.ValidationType = ValidationType.Schema;

            booksSettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler);

            try
            {
                XmlReader books = XmlReader.Create(xml, booksSettings);
                while (books.Read())
                {
                }
            }
            catch (Exception) { }
        }

        public void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
        {
            if (e.Severity == XmlSeverityType.Warning)
            {
                Console.Write("WARNING: ");
                Console.WriteLine(e.Message);
            }
            else if (e.Severity == XmlSeverityType.Error)
            {
                Console.Write("ERROR: ");
                Console.WriteLine(e.Message);
                this.check = true;
            }
        }
    }
    
}
